import * as service from '../services';
import { PAGE_NUM, PAGE_SIZE } from '@/constants'

export default {
  namespace: 'blockList',
  state: {
    list: [],
    total: null,
    page: null,
    timeoutId: null,
    checked: true,
  },
  reducers: {
    save(state, { payload: { list, total, page}}) {
      return { ...state, list, total, page };
    },
    setTimeoutId(state, { payload: { timeoutId } }) {
      if (!timeoutId) {
        clearTimeout(state.timeoutId);
      }
      return { ...state, timeoutId };
    },
    setChecked(state, { payload: { checked } }) {
      return { ...state, checked };
    }
  },
  effects: {
    *fetch({ payload: { page = PAGE_NUM, size = PAGE_SIZE}}, {call, put}) {
      const response = yield call(service.getBlockList, page, size);
      const result = response.data;
      yield put({
        type: 'save',
        payload: {
          list: result.data,
          total: result.total,
          page: result.pageNum,
        }
      })
    }
  },
};
